package com.example.teesystem.controller;


import cn.hutool.core.lang.Validator;
import com.example.teesystem.common.cache.CourseCache;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.vo.paperFeedback.PaperFeedbackInsertVo;
import com.example.teesystem.entity.vo.paperFeedback.PaperFeedbackUpdateVo;
import com.example.teesystem.service.PaperFeedbackService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping(value = "/tee/paperFeedback",produces = "application/json; charset = utf-8")
@RequiredArgsConstructor
@Validated
public class PaperFeedbackController {
    private final PaperFeedbackService paperFeedbackService;

    @PostMapping("/getPapersByPaperIdAndCourseId")
    public Response getPaperByPaperIdAndCourseId(@RequestParam("courseId") int courseId, @RequestParam("paperId") int paperId, @RequestParam("isHandle") int isHandle){
        if(isHandle!=0 && isHandle!=1) {
            return Response.error(ResultCode.PAPER_FEEDBACK_PARAMETER_ERROR.getMsg(),ResultCode.PAPER_FEEDBACK_PARAMETER_ERROR.getCode());
        }
        return paperFeedbackService.getPaperFeedBackByPaperIdAndCourseId(paperId, courseId , isHandle);
    }

    /**
     * 上传试卷反馈
     * @param files
     * @param courseId
     * @param paperId
     * @param message
     * @return
     */
    @PostMapping(value = "/upload")
    public Response upload(MultipartFile[] files, @RequestParam("courseId") int courseId, @RequestParam("paperId") int paperId,
                           @RequestParam("message") String message){
        PaperFeedbackInsertVo paperFeedbackInsertVo;
        if(files == null) {
            paperFeedbackInsertVo = new PaperFeedbackInsertVo(paperId, message, courseId);
        }
        else{
            paperFeedbackInsertVo = new PaperFeedbackInsertVo(paperId, message, courseId, files);
        }
        return paperFeedbackService.PaperFeedbackInsert(paperFeedbackInsertVo);
    }

    /**
     * 处理反馈信息
     * @param paperFeedbackUpdateVo
     * @return
     */
    @PostMapping(value = "/handle")
    public Response handle(@Valid @RequestBody(required = false)PaperFeedbackUpdateVo paperFeedbackUpdateVo){
        Integer id = paperFeedbackUpdateVo.getId();
        String handleMessage = paperFeedbackUpdateVo.getHandleMessage();
        if(Validator.isNull(id) || Validator.isNull(handleMessage)) {
            return Response.error(ResultCode.PAPER_FEEDBACK_PARAMETER_MISS_ERROR.getMsg(), ResultCode.PAPER_FEEDBACK_PARAMETER_MISS_ERROR.getCode());
        }
        return paperFeedbackService.PaperFeedbackHandle(paperFeedbackUpdateVo);
    }


}
